package com.campsys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campsys.common.Result;
import com.campsys.entity.TeachingUnit;
import com.campsys.entity.User;
import com.campsys.service.StudentService;
import com.campsys.service.dto.StudentCourseDTO;
import com.campsys.service.dto.StudentRollCallDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/student/learning")
@Slf4j
public class StudentLearningController {
    
    @Autowired
    private StudentService studentService;
    
    @RequiresRoles("student")
    @RequiresPermissions("learning:view")
    @PostMapping("/courses")
    public Result getMyCourses(@RequestParam Integer pageNum,
                             @RequestParam Integer pageSize) {
        User student = (User) SecurityUtils.getSubject().getPrincipal();
        Page<StudentCourseDTO> page = new Page<>(pageNum, pageSize);
        return Result.ok(studentService.getMyCourses(page, student.getId()));
    }
    
    @RequiresRoles("student")
    @RequiresPermissions("learning:view")
    @PostMapping("/course/{courseId}/units")
    public Result getCourseUnits(@PathVariable Long courseId,
                                @RequestParam Integer pageNum,
                                @RequestParam Integer pageSize) {
        User student = (User) SecurityUtils.getSubject().getPrincipal();
        Page<TeachingUnit> page = new Page<>(pageNum, pageSize);
        return Result.ok(studentService.getCourseUnits(page, courseId, student.getId()));
    }
    
    @RequiresRoles("student")
    @RequiresPermissions("learning:view")
    @PostMapping("/unit/{unitId}")
    public Result getUnitLearning(@PathVariable Long unitId) {
        User student = (User) SecurityUtils.getSubject().getPrincipal();
        return Result.ok(studentService.getUnitLearning(unitId, student.getId()));
    }
    
    @RequiresRoles("student")
    @RequiresPermissions("learning:start")
    @PostMapping("/unit/{unitId}/start")
    public Result startLearning(@PathVariable Long unitId) {
        User student = (User) SecurityUtils.getSubject().getPrincipal();
        studentService.startLearning(unitId, student.getId());
        return Result.ok();
    }
    
    @RequiresRoles("student")
    @RequiresPermissions("learning:complete")
    @PostMapping("/unit/{unitId}/complete")
    public Result completeLearning(@PathVariable Long unitId) {
        User student = (User) SecurityUtils.getSubject().getPrincipal();
        studentService.completeLearning(unitId, student.getId());
        return Result.ok();
    }
    
    @RequiresRoles("student")
    @RequiresPermissions("learning:download")
    @GetMapping("/unit/{unitId}/document")
    public Result getDocumentUrl(@PathVariable Long unitId) {
        User student = (User) SecurityUtils.getSubject().getPrincipal();
        return Result.ok(studentService.getDocumentUrl(unitId, student.getId()));
    }
    
    @RequiresRoles("student")
    @RequiresPermissions("learning:download")
    @GetMapping("/unit/{unitId}/video")
    public Result getVideoUrl(@PathVariable Long unitId) {
        User student = (User) SecurityUtils.getSubject().getPrincipal();
        return Result.ok(studentService.getVideoUrl(unitId, student.getId()));
    }
    
    @RequiresRoles("student")
    @RequiresPermissions("learning:view")
    @PostMapping("/course/{courseId}/roll-calls")
    public Result getRollCallList(@PathVariable Long courseId,
                                @RequestParam Integer pageNum,
                                @RequestParam Integer pageSize) {
        User student = (User) SecurityUtils.getSubject().getPrincipal();
        Page<StudentRollCallDTO> page = new Page<>(pageNum, pageSize);
        return Result.ok(studentService.getRollCallList(page, courseId, student.getId()));
    }
    
    @RequiresRoles("student")
    @RequiresPermissions("learning:sign")
    @PostMapping("/roll-call/{rollCallId}/sign")
    public Result signRollCall(@PathVariable Long rollCallId) {
        User student = (User) SecurityUtils.getSubject().getPrincipal();
        studentService.signRollCall(rollCallId, student.getId());
        return Result.ok();
    }
} 